<?php
defined('IN_PHPCMS') or exit('No permission resources.');
error_reporting(E_ALL ^E_NOTICE);
if (isset($set_modules) && $set_modules == TRUE)
{
    $i = isset($modules) ? count($modules) : 0;

    $modules[$i]['code']    = basename(__FILE__, '.class.php');
    $modules[$i]['name']    = L('tendirect', '', 'pay');   
    $modules[$i]['desc']    = L('tendirect_tip', '', 'pay');
    $modules[$i]['is_cod']  = '0';
    $modules[$i]['is_online']  = '1';
    $modules[$i]['author']  = 'QQ：40386277';
    $modules[$i]['website'] = 'http://www.zfbjk.com';
    $modules[$i]['version'] = '1.1.0';
    $modules[$i]['config']  = array(
     	array('name' => 'tendirect_account','type' => 'text','value' => ''),
        array('name' => 'alidirect_pid','type' => 'text','value' => ''),
        array('name' => 'alidirect_key','type' => 'text','value' => ''),
    );

    return;
}
pc_base::load_app_class('pay_abstract','','0');

class tendirect extends paymentabstract{
	
	public function __construct($config = array()) {	
		if (!empty($config)) $this->set_config($config);
      
		$this->config['gateway_url'] = 'https://www.tenpay.com/v2/account/pay/paymore_cft.shtml';
		$this->config['gateway_method'] = 'GET';
		pc_base::load_app_func('alipay');
	}

	public function getpreparedata() {		
		$tenpay = $this->config['tendirect_account'];
		$payAmount = $this->product_info['price'];
		$title = $this->order_info['id'];
		$prepare_data['data'] = $tenpay.'&'.$payAmount.'&'.$title;
		$prepare_data['validate'] = md5($tenpay.'&'.$payAmount.'&'.$title);
		return $prepare_data;
	}
	
	/**
	 * 客户端接收数据
	 * 状态码说明  （0 交易完成 1 交易失败 2 交易超时 3 交易处理中 4 交易未支付）
	 */
    public function receive() {
    	$receive_data = $this->filterParameter($_POST);
    	$receive_data = arg_sort($receive_data);
    	if ($receive_data) {
			$tradeNo     =trim($receive_data['tradeNo']);
			$money   =trim($receive_data['Money']);  
			$title =trim($receive_data['title']);
			$memo =trim($receive_data['memo']);
			$Sign  =trim($receive_data['Sign']);
			$md5string=strtoupper(md5($this->config['alidirect_pid'].$this->config['alidirect_key'].$tradeNo.$money.$title.$memo));
			if ($Sign==$md5string) {
				$return_data['order_id'] = $title;
				$return_data['order_total'] = $money;
				$return_data['price'] = $money;
				$return_data['order_status'] = 0;
				return $return_data;
			} else { 
				exit('Fail');
				return false;
			}
		} else {
			
			error_log(date('m-d H:i:s',SYS_TIME).'| GET: no return |'."\r\n", 3, CACHE_PATH.'pay_error_log.php');
			exit('Fail');
			return false;
		}   	
    }	

    /**
	 * POST接收数据
	 * 状态码说明  （0 交易完成 1 交易失败 2 交易超时 3 交易处理中 4 交易未支付）
	 */
    public function notify() {
    	$receive_data = $this->filterParameter($_POST);
    	$receive_data = arg_sort($receive_data);
    	if ($receive_data) {
			$tradeNo     =trim($receive_data['tradeNo']);
			$money   =trim($receive_data['Money']);  
			$title =trim($receive_data['title']);
			$memo =trim($receive_data['memo']);
			$Sign  =trim($receive_data['Sign']);
			$md5string=strtoupper(md5($this->config['alidirect_pid'].$this->config['alidirect_key'].$tradeNo.$money.$title.$memo));
			if ($Sign==$md5string) {
				$return_data['order_id'] = $title;
				$return_data['order_total'] = $money;
				$return_data['price'] = $money;
				$return_data['order_status'] = 0;
				return $return_data;
			} else { 
				exit('Fail');
				return false;
			}
		} else {
			
			error_log(date('m-d H:i:s',SYS_TIME).'| notify: no return |'."\r\n", 3, CACHE_PATH.'pay_error_log.php');
			exit('Fail');
			return false;
		}  	
    }
    	
    /**
     * 相应服务器应答状态
     * @param $result
     */
    public function response($result) {
    	if (FALSE == $result) echo 'ok';
		else echo 'success';
    }
    
    /**
     * 返回字符过滤
     * @param $parameter
     */
	private function filterParameter($parameter)
	{
		$para = array();
		foreach ($parameter as $key => $value)
		{
			if ('sign' == $key || 'sign_type' == $key || '' == $value || 'm' == $key  || 'a' == $key  || 'c' == $key   || 'code' == $key ) continue;
			else $para[$key] = $value;
		}
		return $para;
	}
}
?>